import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '../stores/user'

// 导入布局组件
import StudentLayout from '../layouts/StudentLayout.vue'

// 导入页面组件
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import StudentHome from '../views/student/StudentHome.vue'
import MerchantDetail from '../views/student/MerchantDetail.vue'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
    meta: { requiresAuth: false }
  },
  {
    path: '/student',
    component: StudentLayout,
    meta: { requiresAuth: true, role: 'STUDENT' },
    children: [
      {
        path: '',
        name: 'StudentHome',
        component: StudentHome
      },
      {
        path: 'merchant/:id',
        name: 'MerchantDetail',
        component: MerchantDetail
      },
      {
        path: 'orders',
        name: 'StudentOrders',
        component: () => import('../views/student/StudentOrders.vue')
      },
      {
        path: 'profile',
        name: 'StudentProfile',
        component: () => import('../views/student/StudentProfile.vue')
      },
      {
        path: 'address',
        name: 'StudentAddress',
        component: () => import('../views/student/StudentAddress.vue')
      },
      {
        path: 'checkout',
        name: 'StudentCheckout',
        component: () => import('../views/student/StudentCheckout.vue')
      }
    ]
  },
  {
    path: '/merchant',
    component: () => import('../layouts/MerchantLayout.vue'),
    meta: { requiresAuth: true, role: 'MERCHANT' },
    children: [
      {
        path: '',
        name: 'MerchantHome',
        component: () => import('../views/merchant/MerchantHome.vue')
      },
      {
        path: 'menu',
        name: 'MerchantMenu',
        component: () => import('../views/merchant/MerchantMenu.vue')
      },
      {
        path: 'orders',
        name: 'MerchantOrders',
        component: () => import('../views/merchant/MerchantOrders.vue')
      },
      {
        path: 'profile',
        name: 'MerchantProfile',
        component: () => import('../views/merchant/MerchantProfile.vue')
      }
    ]
  },
  {
    path: '/delivery',
    component: () => import('../layouts/DeliveryLayout.vue'),
    meta: { requiresAuth: true, role: 'DELIVERY' },
    children: [
      {
        path: '',
        name: 'DeliveryHome',
        component: () => import('../views/delivery/DeliveryHome.vue')
      },
      {
        path: 'orders',
        name: 'DeliveryOrders',
        component: () => import('../views/delivery/DeliveryOrders.vue')
      },
      {
        path: 'profile',
        name: 'DeliveryProfile',
        component: () => import('../views/delivery/DeliveryProfile.vue')
      }
    ]
  },
  {
    path: '/admin',
    component: () => import('../layouts/AdminLayout.vue'),
    meta: { requiresAuth: true, role: 'ADMIN' },
    children: [
      {
        path: '',
        name: 'AdminHome',
        component: () => import('../views/admin/AdminHome.vue')
      },
      {
        path: 'users',
        name: 'AdminUsers',
        component: () => import('../views/admin/AdminUsers.vue')
      },
      {
        path: 'merchants',
        name: 'AdminMerchants',
        component: () => import('../views/admin/AdminMerchants.vue')
      },
      {
        path: 'orders',
        name: 'AdminOrders',
        component: () => import('../views/admin/AdminOrders.vue')
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/NotFound.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  
  // 检查是否需要认证
  if (to.meta.requiresAuth) {
    if (!userStore.isLoggedIn) {
      next('/login')
      return
    }
    
    // 检查角色权限
    if (to.meta.role && userStore.userRole !== to.meta.role) {
      next('/login')
      return
    }
  }
  
  // 如果已登录且访问登录/注册页面，且已有明确角色，重定向到对应首页
  if (userStore.isLoggedIn && userStore.userRole && (to.path === '/login' || to.path === '/register')) {
    switch (userStore.userRole) {
      case 'STUDENT':
        next('/student')
        break
      case 'MERCHANT':
        next('/merchant')
        break
      case 'DELIVERY':
        next('/delivery')
        break
      case 'ADMIN':
        next('/admin')
        break
      default:
        next('/student')
    }
    return
  }
  
  next()
})

export default router
